<?php

namespace App\Http\Controllers\Api;

use App\apitransifer\ArticleTransfer;
use Illuminate\Http\Request;
use Modules\Shop\Entities\Article;

class ArticleController extends Controller
{
    /**
     *
     * 获取多条文章接口
     *
     * @return mixed
     *
     */
    public function articles()
    {
        //获取get参数中的cid分类id
        //query()可以用来获取get参数中的指定的值,
        //第二个参数可以设置取不到的时候使用的默认值
        $cid = \request()->query('cid', 0);
        if ($cid) {
            $article = Article::where('category_id', $cid)->paginate(2);
        } else {
            $article = Article::paginate(2);
        }

        return $this->response->paginator($article, new ArticleTransfer());
    }

    public function hotArticle(){
        return $this->response->collection(Article::where('iscommend',1)->get(), new ArticleTransfer());
    }
    //获取单个文章接口
    public function article($id){
        //找到对应文章id数据并且返回
        $article = Article::find($id);
        return $this->response->item($article,new ArticleTransfer());
    }
}
